<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style>
body {
  font-family: Helvetica, arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  padding-top: 10px;
  padding-bottom: 10px;
  background-color: white;
  padding: 30px; }

body > *:first-child {
  margin-top: 0 !important; }
body > *:last-child {
  margin-bottom: 0 !important; }

a {
  color: #4183C4; }
a.absent {
  color: #cc0000; }
a.anchor {
  display: block;
  padding-left: 30px;
  margin-left: -30px;
  cursor: pointer;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0; }

h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  cursor: text;
  position: relative; }

h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor {
  background: url() no-repeat 10px center;
  text-decoration: none; }

h1 tt, h1 code {
  font-size: inherit; }

h2 tt, h2 code {
  font-size: inherit; }

h3 tt, h3 code {
  font-size: inherit; }

h4 tt, h4 code {
  font-size: inherit; }

h5 tt, h5 code {
  font-size: inherit; }

h6 tt, h6 code {
  font-size: inherit; }

h1 {
  font-size: 28px;
  color: black; }

h2 {
  font-size: 24px;
  border-bottom: 1px solid #cccccc;
  color: black; }

h3 {
  font-size: 18px; }

h4 {
  font-size: 16px; }

h5 {
  font-size: 14px; }

h6 {
  color: #777777;
  font-size: 14px; }

p, blockquote, ul, ol, dl, li, table, pre {
  margin: 15px 0; }

hr {
  background: transparent url() repeat-x 0 0;
  border: 0 none;
  color: #cccccc;
  height: 4px;
  padding: 0;
}

body > h2:first-child {
  margin-top: 0;
  padding-top: 0; }
body > h1:first-child {
  margin-top: 0;
  padding-top: 0; }
  body > h1:first-child + h2 {
    margin-top: 0;
    padding-top: 0; }
body > h3:first-child, body > h4:first-child, body > h5:first-child, body > h6:first-child {
  margin-top: 0;
  padding-top: 0; }

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
  margin-top: 0;
  padding-top: 0; }

h1 p, h2 p, h3 p, h4 p, h5 p, h6 p {
  margin-top: 0; }

li p.first {
  display: inline-block; }
li {
  margin: 0; }
ul, ol {
  padding-left: 30px; }

ul :first-child, ol :first-child {
  margin-top: 0; }

dl {
  padding: 0; }
  dl dt {
    font-size: 14px;
    font-weight: bold;
    font-style: italic;
    padding: 0;
    margin: 15px 0 5px; }
    dl dt:first-child {
      padding: 0; }
    dl dt > :first-child {
      margin-top: 0; }
    dl dt > :last-child {
      margin-bottom: 0; }
  dl dd {
    margin: 0 0 15px;
    padding: 0 15px; }
    dl dd > :first-child {
      margin-top: 0; }
    dl dd > :last-child {
      margin-bottom: 0; }

blockquote {
  border-left: 4px solid #dddddd;
  padding: 0 15px;
  color: #777777; }
  blockquote > :first-child {
    margin-top: 0; }
  blockquote > :last-child {
    margin-bottom: 0; }

table {
  padding: 0;border-collapse: collapse; }
  table tr {
    border-top: 1px solid #cccccc;
    background-color: white;
    margin: 0;
    padding: 0; }
    table tr:nth-child(2n) {
      background-color: #f8f8f8; }
    table tr th {
      font-weight: bold;
      border: 1px solid #cccccc;
      margin: 0;
      padding: 6px 13px; }
    table tr td {
      border: 1px solid #cccccc;
      margin: 0;
      padding: 6px 13px; }
    table tr th :first-child, table tr td :first-child {
      margin-top: 0; }
    table tr th :last-child, table tr td :last-child {
      margin-bottom: 0; }

img {
  max-width: 100%; }

span.frame {
  display: block;
  overflow: hidden; }
  span.frame > span {
    border: 1px solid #dddddd;
    display: block;
    float: left;
    overflow: hidden;
    margin: 13px 0 0;
    padding: 7px;
    width: auto; }
  span.frame span img {
    display: block;
    float: left; }
  span.frame span span {
    clear: both;
    color: #333333;
    display: block;
    padding: 5px 0 0; }
span.align-center {
  display: block;
  overflow: hidden;
  clear: both; }
  span.align-center > span {
    display: block;
    overflow: hidden;
    margin: 13px auto 0;
    text-align: center; }
  span.align-center span img {
    margin: 0 auto;
    text-align: center; }
span.align-right {
  display: block;
  overflow: hidden;
  clear: both; }
  span.align-right > span {
    display: block;
    overflow: hidden;
    margin: 13px 0 0;
    text-align: right; }
  span.align-right span img {
    margin: 0;
    text-align: right; }
span.float-left {
  display: block;
  margin-right: 13px;
  overflow: hidden;
  float: left; }
  span.float-left span {
    margin: 13px 0 0; }
span.float-right {
  display: block;
  margin-left: 13px;
  overflow: hidden;
  float: right; }
  span.float-right > span {
    display: block;
    overflow: hidden;
    margin: 13px auto 0;
    text-align: right; }

code, tt {
  margin: 0 2px;
  padding: 0 5px;
  white-space: nowrap;
  border: 1px solid #eaeaea;
  background-color: #f8f8f8;
  border-radius: 3px; }

pre code {
  margin: 0;
  padding: 0;
  white-space: pre;
  border: none;
  background: transparent; }

.highlight pre {
  background-color: #f8f8f8;
  border: 1px solid #cccccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px; }

pre {
  background-color: #f8f8f8;
  border: 1px solid #cccccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px; }
  pre code, pre tt {
    background-color: transparent;
    border: none; }

sup {
    font-size: 0.83em;
    vertical-align: super;
    line-height: 0;
}
* {
	-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
    body {
        width: 854px;
        margin:0 auto;
    }
}
@media print {
	table, pre {
		page-break-inside: avoid;
	}
	pre {
		word-wrap: break-word;
	}
}
</style>
<title>请求与输入</title>

</head>
<body>
<h1>请求与输入</h1>

<ul>
<li><a href="#basic-input">基本输入</a></li>
<li><a href="#cookies">Cookies</a></li>
<li><a href="#old-input">旧输入</a></li>
<li><a href="#files">文件</a></li>
<li><a href="#request-information">请求信息</a></li>
</ul>


<p><a name="basic-input"></a></p>

<h2>基本输入</h2>

<p>您可以通过几个简单的函数获取用户的输入。您不需要担心请求所使用的 HTTP 方法，因为所有方式的输入都可以通过相同的方式获取。</p>

<p><strong>获取一个输入变量</strong></p>

<pre><code>$name = Input::get('name');
</code></pre>

<p><strong>当输入变量不存在时获取一个默认值</strong></p>

<pre><code>$name = Input::get('name', 'Sally');
</code></pre>

<p><strong>检查一个输入变量是否存在</strong></p>

<pre><code>if (Input::has('name'))
{
    //
}
</code></pre>

<p><strong>获取请求的所有输入变量</strong></p>

<pre><code>$input = Input::all();
</code></pre>

<p><strong>获取请求的部分输入变量</strong></p>

<pre><code>$input = Input::only('username', 'password');

$input = Input::except('credit_card');
</code></pre>

<p>一些 JavaScript 类库比如 Backbone 会以 JSON 格式向应用发送请求，您可以像往常一样通过 <code>Input::get</code> 获取数据。</p>

<p><a name="cookies"></a></p>

<h2>Cookies</h2>

<p>所有由 Laravel 框架产生的 Cookie 都是被加密并根据一个认证码签名，这意味着如果客户改变了它，将变得不可用。</p>

<p><strong>获取一个 Cookie 变量</strong></p>

<pre><code>$value = Cookie::get('name');
</code></pre>

<p><strong>在响应中附加一个 Cookie</strong></p>

<pre><code>$response = Response::make('Hello World');

$response-&gt;withCookie(Cookie::make('name', 'value', $minutes));
</code></pre>

<p><strong>创建一个永不过期的 Cookie</strong></p>

<pre><code>$cookie = Cookie::forever('name', 'value');
</code></pre>

<p><a name="old-input"></a></p>

<h2>旧输入</h2>

<p>您可能希望保持输入变量直到下一次请求。比如，您需要在验证表单错误后重新填充表单。</p>

<p><strong>闪存所有 Input 变量到 Session</strong></p>

<pre><code>Input::flash();
</code></pre>

<p><strong>闪存部分 Input 变量到 Session</strong></p>

<pre><code>Input::flashOnly('username', 'email');

Input::flashExcept('password');
</code></pre>

<p>因为您经常希望在跳转到前一个页面的时候闪存 Input 变量，您可以在跳转的使用链式的方法：</p>

<pre><code>return Redirect::to('form')-&gt;withInput();

return Redirect::to('form')-&gt;withInput(Input::except('password'));
</code></pre>

<blockquote><p><strong>注意:</strong> 您可能希望使用 <a href="/docs/session">Session</a> 类来根据请求闪存其他数据。</p></blockquote>

<p><strong>获取旧输入</strong></p>

<pre><code>Input::old('username');
</code></pre>

<p><a name="files"></a></p>

<h2>文件</h2>

<p><strong>获取一个已上传的文件</strong></p>

<pre><code>$file = Input::file('photo');
</code></pre>

<p><strong>检查一个文件是否已上传</strong></p>

<pre><code>if (Input::hasFile('photo'))
{
    //
}
</code></pre>

<p>通过 <code>file</code> 函数返回的对象是一个 <code>Symfony\Component\HttpFoundation\File\UploadedFile</code> 类的实例，这个类扩展了 PHP 的 <code>SplFileInfo</code> 类，并且提供了众多与文件交互的方法。</p>

<p><strong>转移一个已上传的文件</strong></p>

<pre><code>Input::file('photo')-&gt;move($destinationPath);

Input::file('photo')-&gt;move($destinationPath, $fileName);
</code></pre>

<p><strong>获取一个已上传文件的目录</strong></p>

<pre><code>$path = Input::file('photo')-&gt;getRealPath();
</code></pre>

<p><strong>获取一个已上传文件的大小</strong></p>

<pre><code>$size = Input::file('photo')-&gt;getSize();
</code></pre>

<p><strong>获取一个已上传文件的 MIME 类型</strong></p>

<pre><code>$mime = Input::file('photo')-&gt;getMimeType();
</code></pre>

<p><a name="request-information"></a></p>

<h2>请求信息</h2>

<p><code>Request</code> 类扩展了 <code>Symfony\Component\HttpFoundation\Request</code> 类并提供了检查 HTTP 请求的众多函数，下面是一些常用代码：</p>

<p><strong>获取请求的 URI</strong></p>

<pre><code>$uri = Request::path();
</code></pre>

<p><strong>检查一个请求的路径是否匹配指定的模式</strong></p>

<pre><code>if (Request::is('admin/*'))
{
    //
}
</code></pre>

<p><strong>获取请求的 URL</strong></p>

<pre><code>$url = Request::url();
</code></pre>

<p><strong>获取请求的指定的 URI 段</strong></p>

<pre><code>$segment = Request::segment(1);
</code></pre>

<p><strong>获取请求的头部</strong></p>

<pre><code>$value = Request::header('Content-Type');
</code></pre>

<p><strong>从 $_SERVER 中获取变量</strong></p>

<pre><code>$value = Request::server('PATH_INFO');
</code></pre>

<p><strong>检查一个请求是否使用 AJAX</strong></p>

<pre><code>if (Request::ajax())
{
    //
}
</code></pre>

<p><strong>检查一个请求是否通过 HTTPS</strong></p>

<pre><code>if (Request::secure())
{
    //
}
</code></pre>
</body>
</html>